/**
 * Hien thi dialog thong bao va cho phep chon level
 */
package view;
import java.awt.Frame;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;

import javax.swing.DefaultListModel;
import javax.swing.ImageIcon;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;

import model.Constant;
import model.database.LevelDB;
import model.database.PlayDB;
import model.entity.Levels;

/**
 * @author heroandtn3
 * @date Oct 25, 2012
 */
public class LevelDlg extends JDialog {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private PlayPanel playPanel;
	private JLabel lbExit;
	private JList<String> jlistLevel;
	private JLabel lbNotice;
	private JLabel lbImg;
	private JScrollPane scrollPane;
	private JLabel lbBackHome;
	private JLabel lbPlay;
	private List<Levels> levels;
	private DefaultListModel<String> jlistLevelModel;

	/**
	 * 
	 */
	public LevelDlg(Frame owner, boolean modal) {
		super(owner, modal);
		// TODO Auto-generated constructor stub
		setUndecorated(true);
		setVisible(false);
		initGUI();
		setLocationRelativeTo(owner);
	}
	
	public void setPlayPanel(PlayPanel playPanel) {
		this.playPanel = playPanel;
	}
	
	private void initGUI() {
		try {
			{
				getContentPane().setLayout(null);
				this.setResizable(false);
				this.setAlwaysOnTop(true);
				getContentPane().setBackground(new java.awt.Color(0,51,51));

				{
					lbPlay = new MyLabelButton(Constant.BUTTON_DIR + "play", "Play this level", false);
					getContentPane().add(lbPlay);
					lbPlay.setBounds(184, 209, 48, 48);
					lbPlay.setBackground(new java.awt.Color(0,0,0));
					lbPlay.addMouseListener(new MouseAdapter() {
						public void mouseClicked(MouseEvent evt) {
							//TODO add your code for lbPlay.mouseClicked
							if (jlistLevel.getSelectedIndex() > -1) {
								playPanel.getPlaying().setLevel(jlistLevel.getSelectedIndex());
								playPanel.setActive(false);
							}
							if (!playPanel.isActive()) {
								// sang level moi hoac bat dau game
								playPanel.initGame();
							}
							setVisible(false);
						}
					});
				}
				{
					lbExit = new MyLabelButton(Constant.BUTTON_DIR + "cancel", "Exit", false);
					getContentPane().add(lbExit);
					lbExit.setBounds(260, 209, 48, 48);
					lbExit.addMouseListener(new MouseAdapter() {
						public void mouseClicked(MouseEvent evt) {
							//TODO add your code for lbExit.mouseClicked
							if (playPanel.isActive()) {
								setVisible(false);
							} else {
								lbNotice.setText("You have not play game yet, please choice Play button!");
							}
							
						}
					});
				}
				{
					lbBackHome = new MyLabelButton(Constant.BUTTON_DIR + "playother", "Back to user menu", false);
					getContentPane().add(lbBackHome);
					lbBackHome.setBounds(341, 209, 48, 48);
					lbBackHome.addMouseListener(new MouseAdapter() {
						public void mouseClicked(MouseEvent evt) {
							//TODO add your code for lbBackHome.mouseClicked
							setVisible(false);
							if (playPanel.isActive()) {
								playPanel.getPlaying().setScore(playPanel.getScore());
								PlayDB pDB = new PlayDB();
								pDB.insertPlay(playPanel.getPlaying());
							}
							playPanel.resetGame();
							playPanel.getMainPanel().switchPanel(UserHomePanel.KEY);
						}
					});
				}
				{
					scrollPane = new JScrollPane();
					getContentPane().add(scrollPane);
					scrollPane.setBounds(30, 58, 91, 202);
					{
						jlistLevelModel = new DefaultListModel<String>();
						jlistLevel = new JList<String>();
						jlistLevel.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
						scrollPane.setViewportView(jlistLevel);
						jlistLevel.setModel(jlistLevelModel);
						
					}
				}
				{
					lbNotice = new JLabel();
					getContentPane().add(lbNotice);
					lbNotice.setBounds(184, 300, 260, 37);
				}
				{
					lbImg = new JLabel(new ImageIcon(Constant.BG_DIR + "level.png"));
					getContentPane().add(lbImg);
					lbImg.setBounds(545, 58, 291, 209);
				}
			}
			{
			}
			{
				this.setSize(Constant.PANEL_WIDTH, 375);
			}
		} catch(Exception e) {
			//e.printStackTrace();
		}
	} // ket thuc phuong thuc iniGUI()

	public void initListLevel() {
		LevelDB lDB = new LevelDB();
		int mapID = playPanel.getPlaying().getMap().getId();
		levels = lDB.getAllLevelByMapID(mapID);
		jlistLevelModel.clear();
		for (int i=0; i<levels.size(); i++) {
			jlistLevelModel.addElement("Level " + i);
		}
		jlistLevel.setSelectedIndex(0);
	}

	/**
	 * @return the jlistLevel
	 */
	public JList<String> getJlistLevel() {
		return jlistLevel;
	}


	
	
}
